package com.woupla.categ;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.jboss.solder.logging.Logger;

import com.woupla.model.Category;

@Stateless
public class CategoryEJB {
	@Inject
	private Logger log;

	@Inject
	private EntityManager em;

	public List<Category> findAllByParent(Category parent) {
		TypedQuery<Category> query;
		if (parent == null) {
			query = em.createNamedQuery("Category.findParentIsNull", Category.class);
		} else {
			query = em.createNamedQuery("Category.findByParent", Category.class);
			query.setParameter("parent", parent);
		}
		List<Category> categs = query.getResultList();
		return categs;

	}

	public List<Category> findAllByParentLabel(String parent) {
		TypedQuery<Category> query;
		if (parent == null) {
			query = em.createNamedQuery("Category.findParentIsNull", Category.class);
		} else {
			query = em.createNamedQuery("Category.findByParentLabel", Category.class);
			query.setParameter("parent", parent);
		}
		List<Category> categs = query.getResultList();
		return categs;

	}

	public Category findByLabel(String parent) {
		TypedQuery<Category> query;
		query = em.createNamedQuery("Category.findByLabel", Category.class);
		query.setParameter("label", parent);
		Category categ = query.getSingleResult();
		return categ;

	}
}
